package com.example.Lezedata.ui.activity.record;

import android.content.Context;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.BidRecordModel;
import com.example.Lezedata.dataModel.GoodsProjectModel;
import com.example.Lezedata.ui.activity.goods.GoodsNameView;
import com.example.Lezedata.util.GBTimeManager;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;


public class PrizesRecordCell extends FrameLayout {
    public PrizesRecordCell(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    TextView tx_time;
    ImageView tx_imageView;
    TextView tx_state;
    TextView tx_code;
    GoodsNameView tx_goodsNameView;
    Bid.UserWinPrizeListResponse.UserWinPrizeItem model;

    public static PrizesRecordCell sharedCell(Context context){
        PrizesRecordCell cell = (PrizesRecordCell) LayoutInflater
                .from(context)
                .inflate(R.layout.prizes_record_cell,null,false);
        return cell;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findView();
    }

    void findView(){
        GoodsProjectModel projectModel = GoodsProjectModel.testGoodsProjectModel();
        tx_time = findViewById(R.id.tx_time);
        tx_imageView = findViewById(R.id.tx_imageView);
        tx_state = findViewById(R.id.tx_state);
        tx_code = findViewById(R.id.tx_code);
        tx_goodsNameView = findViewById(R.id.tx_goodsNameView);
        tx_goodsNameView.setModelForNameWithDetail(projectModel.infoModel.goodsName,projectModel.infoModel.goodsDetail);
        Glide.with(tx_imageView).load(projectModel.infoModel.goodsIcon).into(tx_imageView);
    }

    public void setModel(Bid.UserWinPrizeListResponse.UserWinPrizeItem model) {
        this.model = model;
        tx_time.setText(String.format(" Time of Raffle: %s", GBTimeManager.timesYMD_HMS(model.getCreateTime())));
        GBUtil.setGoodsImage(tx_imageView,model.getImgUrl());
        tx_goodsNameView.setModelForNameWithDetail(GBUtil.RoundToString(model.getBidRound()),model.getGoodsName());

        String bStr1 = String.format("%s",model.getBidCode());
        String allS1 = String.format("Wish Code:%s",bStr1);
        int index1 = allS1.indexOf(bStr1);
        SpannableString spannableString1 = GBUtil.textAddColor(allS1,getResources().getColor(R.color.app_color_main),index1,index1+bStr1.length());
        tx_code.setText(spannableString1);

        switch (model.getRedeemStatus()){
            case RS_DEFAULT:
            case RS_REDEEMED:
                tx_state.setText("Redeemed");
                tx_state.setBackground(getResources().getDrawable(R.drawable.corner_drak_left_top_bom_background_10dp));
            case RS_UN_REDEEM:
                tx_state.setText("Unredeemed");
                tx_state.setBackground(getResources().getDrawable(R.drawable.corner_yellow_left_top_bom_background_10dp));
            case RS_IN_PROGRESS:
                tx_state.setText("In progress");
                tx_state.setBackground(getResources().getDrawable(R.drawable.corner_miancolor_left_top_bom_background_10dp));
        }
    }
}
